﻿using B.R.RabbitRoot.Domain;
using B.R.RabbitRoot.ErrorCode;
using B.R.RabbitRoot.Infrastructure.Interface;
using B.R.RabbitRoot.Write.API.Applications.Commands.ImgCommands;
using MediatR;

namespace B.R.RabbitRoot.Write.API.Applications.Commandhandlers.ImgCommandHandlers
{
    /// <summary>
    /// 图片的添加命令
    /// </summary>
    public class ImageTableCreamCommandHandler : IRequestHandler<ImageTableCreamCommand, APIResult<int>>
    {
        /// <summary>
        /// 图片仓储
        /// </summary>
        private readonly IBaserepository<ImageTable> _imageTableRedsitoty;

        // <summary>
        /// 构造函数
        /// </summary>
        public ImageTableCreamCommandHandler(IBaserepository<ImageTable> imageTableRedsitoty)
        {
            _imageTableRedsitoty = imageTableRedsitoty;
        }
        /// <summary>
        /// 图片的添加命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<int>> Handle(ImageTableCreamCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            result.Code=ResultCode.添加成功;
            result.Message = ResultCode.添加成功.ToString();
            result.Data = await _imageTableRedsitoty.AddAsync(entity: new ImageTable
            {
                SourceInfoId=request.SourceInfoId,
                PrimaryCategory = request.PrimaryCategory,
                SecondaryCategory = request.SecondaryCategory,
                ImageUrl = request.ImageUrl
            });
            return await Task.FromResult(result);
        }
    }
}
